import utils from "../../utils/index";
import {
  fansModel
} from "../../apis/index"

import regeneratorRuntime from '../../libs/wxPromise.min';
import {
  InterAd
} from '../../utils/InterAd'
Page({
  data: {
    tabs: [{
      name: '活跃粉丝团'
    }, {
      name: '我的粉丝团'
    }],
    tabsIndex: 0,
    page1: {
      page: 1,
      pageSize: 10
    },
    myFans: [],
    page2: {
      page: 1,
      pageSize: 10
    },
    teamFans: [],
    keywords:'',
    page: {
      page: 1,
      pageSize: 10
    },
    searchlist: [],
    placeholder:'输入粉丝团名称',
    search:false
  },
  mixins: [require('../../myMixin.js')],
  onLoad: function (options) {
    let {fid='',type='',sid=''} = options
    if(type&&fid){//加入集结的
      utils.goUrl(`../supstar/index?fid=${fid}&&type=${type}&&sid=${sid}`)
      return false
    }
  },
  onReady: function () {

      let interAd = new InterAd('7fb828e435af61d59234ae3978c20221')
      interAd.showAd()
   
  },
  onShow: function () {
    this.getInitStarList()
  },
  onShareAppMessage: function () {

  },
  tabClick(e) {
    let index = e.detail.index
    this.setData({
      tabsIndex: index
    }, () => {
      let {
        myFans,
        teamFans,
        tabsIndex
      } = this.data
      if (tabsIndex == 0 && teamFans.length == 0) {
        this.getInitStarList()
      } else if (tabsIndex == 1 && myFans.length == 0) {
        this.getInitStarList()
      }
    })

  },
  getInitStarList() {
    let {
      page1,
      page2,
      myFans,
      teamFans,
      tabsIndex
    } = this.data
    if (tabsIndex == 0) {
      teamFans = []
      page2.page = 1
      this.data.teamFans = teamFans
      this.data.page2 = page2
      this.getTeamFansList()
    } else if (tabsIndex == 1) {
      myFans = []
      page1.page = 1
      this.data.myFans = myFans
      this.data.page1 = page1
      this.getMyFansList()
    }
  },
  getMyFansList() {
    let {
      page,
      pageSize
    } = this.data.page1
    let {
      myFans
    } = this.data
    let param = {
      page: page,
      pageSize: pageSize,
      sid: ''
    }
    utils.showLoading()
    fansModel.myFans(param).then(res => {
      let _list = res.list
      myFans = myFans.concat(_list)
      this.computedPage('page1', res.total)
      this.setData({
        myFans: myFans
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  moreMyFansData() {
    this.loadMoreData('page1', this.getMyFansList)
  },
  getTeamFansList() {
    let {
      page,
      pageSize
    } = this.data.page2
    let {
      teamFans
    } = this.data
    let param = {
      page: page,
      pageSize: pageSize,
      name: ''
    }
    utils.showLoading()
    fansModel.fansTeams(param).then(res => {
      let _list = res.list
      teamFans = teamFans.concat(_list)
      this.computedPage('page2', res.total)
      this.setData({
        teamFans: teamFans
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  moreTeamFansData() {
    this.loadMoreData('page2', this.getTeamFansList)
  },
  goUrl(e){
    let url = e.currentTarget.dataset.url
    utils.goUrl(url)
  },
  viewClubDetail(e){
    let item = e.currentTarget.dataset.item
    utils.goUrl(`../supstar/index?fid=${item.id}`)
  },
  onFocus(){
    this.setData({
      search:true
    })
  },
  async addFanClub(e){
    let item = e.currentTarget.dataset.item
    let fid = item.id
    if(item.type==1){
      let confirm =await utils.showModalContent(``,`你已加入${item.name}粉丝团`, '确定', false, '')
      return false
    }
    let confirm =await utils.showModalContent(`确定加入 ${item.name}?`,`所属名星: ${item.sname}`, '确定', true, '取消')
    
    if(confirm){
      fansModel.addFansClub({fid}).then(res=>{
        utils.showToast('成功加入该粉丝团','none')
        utils.goUrl(`../supstar/index?fid=${fid}`)
      })
    }
  },
  getSearchList(keywords) {
    let {
      page,
      pageSize
    } = this.data.page
    let { searchlist } = this.data

    let param = {
      page: page,
      pageSize: pageSize,
      name: keywords.trim()
    }
    utils.showLoading()
    fansModel.fansTeams(param).then(res => {
      let _list = res.list
      searchlist = searchlist.concat(_list)
      this.computedPage('page', res.total)
      this.setData({
        searchlist: searchlist
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  moreSearchListData() {
    this.loadMoreData('page', this.getSearchList)
  },
  onInputChange(e){
    let value = e.detail.value
    this.setData({
      keywords:value
    })
  },
  hideSearch(){
    this.data.page.page = 1
    this.setData({
      searchlist:[],
      search:false,
      keywords:''
    })
  },
  searchHandle(e){
    let {value:keywords} = e.detail
    this.timer = this.timer || null
    this.setData({
      keywords:keywords
    })
    clearTimeout(this.timer)
    this.timer = setTimeout(()=>{
      this.data.searchlist = []
      this.data.page.page = 1
      this.getSearchList(keywords)
    },500)
  },
  clearKeyWords(){
    this.data.page.page = 1
    this.setData({
      keywords:'',
      searchlist:[]
    })
  },
  tapHandler(e){
    let item = e.currentTarget.dataset.item
    let { type } = this.data

    if(type=='1'){ //
      utils.redirectTo({
        url: `../idol/index?sid=${item.id}`
      })
    }else if(type=='2'){ //数据回传
      app.globalData.search_data = item
      utils.navigateBack({
        delta:1
      })
     
    }
  }
})